//
//  PayWebViewController.m
//  cityStar
//
//  Created by Hancong on 15/1/27.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "PayWebViewController.h"
#import "WebViewJavascriptBridge.h"
#import "MyOrdersViewController.h"

@interface PayWebViewController ()<UIWebViewDelegate>

@property (nonatomic,weak) IBOutlet UIWebView *webView;
@property (nonatomic,strong) NSString *order;
@property (nonatomic,strong) NSString *price;
@property WebViewJavascriptBridge* bridge;

@end

@implementation PayWebViewController
-(instancetype)initWithOrder:(NSString *)order andPrice:(NSString *)price
{
    self = [super init];
    if (self) {
        self.order = order;
        self.price = price;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setUpView];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(jumpToOrderWithGet) name:@"JUMPTOORDER_WAITGET" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(jumpToOrderWithPay) name:@"JUMPTOORDER_WAITPAY" object:nil];
}

-(void)setUpView
{
    [self addBackButton];
    self.title = @"支付宝付款";
    
//    NSString *strUrl = [NSString stringWithFormat:@"http://mobile.icszx.com/pay/Index/alipayapi/no/%@/name/%@/price/%@",self.order,@"城市之星订单付款",self.price];
//    ;
//    NSURL *url = [NSURL URLWithString:[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//    self.webView.delegate = self;
//    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
    
    
    
    if (_bridge) { return; }
    
//    UIWebView* webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
//    [self.view addSubview:webView];
    
    [WebViewJavascriptBridge enableLogging];
    
    _bridge = [WebViewJavascriptBridge bridgeForWebView:self.webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"ObjC received message from JS: %@", data);
        responseCallback(@"Response for message from ObjC");
    }];
    
    [_bridge registerHandler:@"WebViewJavascriptBridgeReady" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"WebViewJavascriptBridgeReady called: %@", data);
        responseCallback(@"Response from testObjcCallback");
    }];
    [self loadExamplePage:self.webView];

}

-(void) webViewDidFinishLoad:(UIWebView *)webView {
    
//    NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
    
    NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
//    DLog(@"1111111111%@",title);

}

- (void)loadExamplePage:(UIWebView*)webView {
//    NSString* htmlPath = [[NSBundle mainBundle] pathForResource:@"ExampleApp" ofType:@"html"];
//    NSString* appHtml = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
//    NSURL *baseURL = [NSURL fileURLWithPath:htmlPath];
//    NSString *strUrl = [NSString stringWithFormat:@"http://mobile.icszx.com/pay/Index/alipayapi/no/%@/name/%@/price/%@",self.order,@"城市之星订单付款",self.price];

//    [webView loadRequest:[NSURLRequest requestWithURL:[NSString stringWithFormat:@"%@",strUrl]]];
//    [webView loadHTMLString:appHtml baseURL:strUrl];
    
    NSString *strUrl = [NSString stringWithFormat:@"http://mobile.icszx.com/pay/Index/alipayapi/no/%@/name/%@/price/%@",self.order,@"城市之星订单付款",self.price];
    ;
    NSURL *url = [NSURL URLWithString:[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//    webView.delegate = self;
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
}

#pragma mark - Action
-(void)jumpToOrderWithGet
{
    DLog(@"%@",@"get");
    MyOrdersViewController *ordersVC = [[MyOrdersViewController alloc]initWithSelectStatues:YES];
    [self.navigationController pushViewController:ordersVC animated:YES];
}

-(void)jumpToOrderWithPay
{
    DLog(@"%@",@"pay");
    MyOrdersViewController *ordersVC = [[MyOrdersViewController alloc]initWithSelectStatues:NO];
    [self.navigationController pushViewController:ordersVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
